package yns.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 针对JDK8之前的JDK
 * @author YNS
 *
 */
public class TimeHelper {
	
	private static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static Calendar calendar;
	
	private TimeHelper() {
		
	}
	
	/**
	 * 获得当前时间，返回格式：2014-12-06 19:33:02
	 * @return
	 */
	public static String getCurrentTime() {
		calendar = Calendar.getInstance();
		return timeFormat.format(calendar.getTime());
	}
	
	/**
	 * 日期字符串转换为日期类型
	 * @param formatDate 格式�?2014-12-06 19:33:02
	 * @return
	 * @throws ParseException
	 */
	public static Date getStringToDate(String formatDate) throws ParseException {
		return timeFormat.parse(formatDate);
	}
	
	/**
	 * 日期类型转换为日期字符串
	 * @param formatDate
	 * @return
	 */
	public static String getDateToString(Date formatDate) {
		return timeFormat.format(formatDate);
	}
	
	
	/**
	 * 获得指定年�?�月、日、时、分、秒的长整型
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static long getTimeToMillis(int year,int month,int day,int hour,int minute,int second) {
		calendar = Calendar.getInstance();
		calendar.set(year, month, day, hour, minute,second);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 获得指定时间的长整型
	 * @param formatDate
	 * @return
	 */
	public static long getTimeToMillis(Date formatDate) {
		calendar = Calendar.getInstance();
		calendar.setTime(formatDate);
		return calendar.getTimeInMillis();
	}
	
	/**
	 * 长整型转换为时间字符串，返回格式�?2014-12-06 19:33:02
	 * @param timeMillis
	 * @return
	 */
	public static String getMillisToTime(long timeMillis) {
		calendar = Calendar.getInstance();
		calendar.setTimeInMillis(timeMillis);
		return timeFormat.format(calendar.getTime());
	}
	
	/**
	 * 日期类型格式化为指定的时间字符串
	 * @param formatStr
	 * @param formatDate
	 * @return
	 */
	public static String getCustomTime(String formatStr,Date formatDate) {
		SimpleDateFormat customTimeFormat = new SimpleDateFormat(formatStr);
		return customTimeFormat.format(formatDate);
	}
	
	/**
	 * 长整型格式化为指定的时间字符�?
	 * @param formatStr
	 * @param formatDate
	 * @return
	 */
	public static String getCustomTime(String formatStr,long formatDate) {
		SimpleDateFormat customTimeFormat = new SimpleDateFormat(formatStr);
		return customTimeFormat.format(formatDate);
	}
}
